<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link href="../../../static/css/style.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="../../../static/js/jquery.js"></script>
    <style type="text/css">

        #treeMenu ul {
            margin-left: 50px;
        }

        #treeMenu a {
            font-size: 14px;
        }

        .hidden {
            display: none;
        }
    </style>

    <script type="text/javascript">
        $(function () {
            let moduleId = 0;
            $.ajax({
                method: "post",
                url: "/job/moduleList2",
                data: {moduleId: moduleId},
                dataType: "JSON",
                async: false,
                success: function (rm) {
                    var map = rm.data;
                    showMap(map);
                },
                error: function (rm) {
                    alert(rm.msg)
                },
            })
            $("#treeMenu").children("li").children(":checkbox").click(function () {
                let flag = $(this).prop("checked");
                $(this).siblings("ul").show();
                $(this).siblings("ul").children("li").children(":checkbox").prop("checked", flag);
            }).siblings("a").click(function () {
                $(this).siblings("ul").toggle();
            });
            $(".zi").click(function () {
                let flag = $(this).prop("checked");
                if(flag){
                    $(this).parent().parent().prev().prev().prop("checked",flag);
                // }else {
                    // $(this).parent().parent().find(".zi").each(function () {
                    //     if ($(this).parent().parent().find(".zi") === false) {
                    //         flag=false;
                    //     }
                    // })
                    // $(this).parent().parent().prev().prev().prop("checked",flag);
                }
            })

        })

        function showMap(map) {
            let str = '';
            for (let key in map) {
                let moduleId = key.split(' ')[0];
                let moduleRname = key.split(' ')[1];
                let list = map[key];
                str += '  <li>\n' +
                    '            <input type="checkbox" id="' + moduleId + '" value="' + moduleId + '"/>\n' +
                    '            <a href="javascript:void(0);" value="' + moduleId + '">' + moduleRname + '</a>\n' +
                    '            <ul class="hidden">';
                for (let i = 0; i < list.length; i++) {
                    str += '                <li>\n' +
                        '                    <input class="zi" type="checkbox" id="' + list[i].moduleId + '" value="' + list[i].moduleId + '"/>\n' +
                        '                    <a href="javascript:void(0);" value="' + list[i].moduleId + '">' + list[i].moduleRname + '</a>\n' +
                        '                </li>';
                }
                str += '            </ul>\n' +
                    '        </li>';
            }
            $("#treeMenu").html(str);
        }


        function saveQuan() {
            let array = [];
            $("input:checked").each(function () {
                let id = $(this).next().attr('value');
                array.push(id);
            });

            var comrid = JSON.parse(localStorage.getItem("sysjobId2"));

            $.ajax({
                type: "post",
                url: "/job/delPermission",
                data: {comrid:comrid},
                dataType: "JSON",
                success: function () {
                    for (let i = 0; i < array.length; i++) {
                        $.ajax({
                            type: "post",
                            url: "/job/addPermission",
                            data: {comrid:comrid,compid:array[i]},
                            dataType: "JSON",
                            success: function (rm) {
                                location.href = "findByPage";
                            },
                            error: function (rm) {
                                alert(rm.msg);
                            }
                        })
                    }
                },
                error: function (rm) {
                    alert(rm.msg);
                }
            })

        }
    </script>
</head>
<body>
<div class="place"><span>位置：</span>
    <ul class="placeul">
        <li><a href="../job/findByPage">系统管理</a></li>
        <li><a href="../job/findByPage">职位管理</a></li>
        <li><a href="../job/positionGrant">赋权</a></li>
    </ul>
</div>
<div class="formbody">
    <div class="formtitle"><span>职位赋权</span></div>
    <ul id="treeMenu" class="forminfo">
        <li>
            <input type="checkbox"/>
            <a href="javascript:void(0);">系统管理</a>

            <ul class="hidden">
                <li>
                    <input type="checkbox" onclick=""/>
                    <a href="../modules/moduleView.html">用户管理</a>
                </li>
                <li>
                    <input type="checkbox" onclick=""/>
                    <a href="../modules/moduleView.html">日志管理</a>
                </li>
                <li>
                    <input type="checkbox" onclick=""/>
                    <a href="../modules/moduleView.html">模块管理</a>
                </li>
            </ul>
        </li>

        <li>
            <input type="checkbox"/>
            <a href="javascript:void(0);">营销管理</a>

            <ul class="hidden">
                <li>
                    <input type="checkbox" onclick=""/>
                    <a href="../modules/moduleView.html">客户管理</a>
                </li>
                <li>
                    <input type="checkbox" onclick=""/>
                    <a href="../modules/moduleView.html">订购单审核</a>
                </li>
                <li>
                    <input type="checkbox" onclick=""/>
                    <a href="../modules/moduleView.html">订购单管理</a>
                </li>
            </ul>
        </li>
        <li>
            <input type="checkbox"/>
            <a href="javascript:void(0);">仓库管理</a>

            <ul class="hidden">
                <li>
                    <input type="checkbox" onclick=""/>
                    <a href="../modules/moduleView.html">仓库管理</a>
                </li>
                <li>
                    <input type="checkbox" onclick=""/>
                    <a href="../modules/moduleView.html">出库管理</a>
                </li>
                <li>
                    <input type="checkbox" onclick=""/>
                    <a href="../modules/moduleView.html">入库管理</a>
                </li>
            </ul>
        </li>
    </ul>

    <input name="" type="button" class="btn" value="确认赋权" onclick="saveQuan()"/>
    <input name="" type="button" class="btn" value="返回" onclick="window.history.go(-1);"/>
</div>
</body>
</html>
